package com.hjduan.csdn.service;


import com.hjduan.csdn.model.Data;
import com.hjduan.csdn.rule.Rule;
import com.hjduan.csdn.rule.RuleException;
import com.hjduan.csdn.utils.DateUtil;
import com.hjduan.csdn.utils.StringUtil;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * Description: 抓取核心类
 * 段浩杰   2017-08-05-22:47
 */
public class GrabService {

    public static List<Data> extract(Rule rule) {

        // 进行对rule的必要校验
        validateRule(rule);

        List<Data> datas = new ArrayList<Data>();
        Data data = null;
        try {
            /**
             * 解析rule
             */
            String url = rule.getUrl();
            String resultTagName = rule.getResultTagName();
            int type = rule.getType();
            int requestType = rule.getRequestMoethod();

            Connection conn = Jsoup.connect(url);


            // 设置请求类型
            Document doc = null;
            switch (requestType) {
                case Rule.GET:
                    doc = conn.timeout(100000).get();
                    break;
                case Rule.POST:
                    doc = conn.timeout(100000).post();
                    break;
            }

            //处理返回数据
            Elements results = new Elements();
            switch (type) {
                case Rule.CLASS:
                    results = doc.getElementsByClass(resultTagName);
                    break;
                case Rule.ID:
                    Element result = doc.getElementById(resultTagName);
                    results.add(result);
                    break;
                case Rule.SELECTION:
                    results = doc.select(resultTagName);
                    break;
                default:
                    //当resultTagName为空时默认去body标签
                    if (StringUtil.isEmpty(resultTagName)) {
                        results = doc.getElementsByTag("body");
                    }
            }

            for (Element result : results) {
                Elements links = result.getElementsByTag("a");

                for (Element link : links) {
                    //必要的筛选
                    String linkHref = link.attr("href");
                    String linkText = link.text();
                    data = new Data();
                    data.setHref(linkHref);
                    data.setLinkName(linkText);
                    data.setTime(DateUtil.getFormatDate());
                    datas.add(data);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return datas;
    }


    /**
     * 对传入的参数进行必要的校验
     */
    private static void validateRule(Rule rule) {
        String url = rule.getUrl();
        if (StringUtil.isEmpty(url)) {
            throw new RuleException("url不能为空！");
        }
        if (!url.startsWith("http://blog.csdn.net/")) {
            throw new RuleException("url的格式不正确！");
        }
    }
}
